#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

//#define MYSQL "192.168.126.1"
#define MYSQL_IP "localhost" 
#define MYSQL_PORT 3306
#define MYSQL_USER "root"
#define MYSQL_PASSWORD "liqiang"
#define MYSQL_DB "mysql"

int main(){
	//mysql_library_init();
	MYSQL * mysql=mysql_init(NULL);
	//mysql_real_connect(mysql,"localhost","root","liqiang","test",3306,NULL,0);
	mysql_real_connect(mysql,MYSQL_IP,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DB,MYSQL_PORT,NULL,0);
	if(mysql==NULL){
		fprintf(stderr, "Failed to connect to database: Error: %s\n",
		          mysql_error(mysql));
		exit(1);
	}
	int rs=mysql_query(mysql,"select * from user");
	int num_fields=mysql_field_count(mysql);
	printf("rs:%d,field count:%d\n",rs,num_fields);
	MYSQL_RES* result;
	MYSQL_ROW row;
	result=mysql_use_result(mysql);


	MYSQL_FIELD* fields;
	fields=mysql_fetch_fields(result);
	int i;
	for(i=0;i<num_fields;i++){
		printf("%s %d\n",fields[i].name,fields[i].type);
	}
	printf("\n");
	i=0;
	while((row=mysql_fetch_row(result))){
		printf("row %d\n",i++);	
		printf("%s %s %s\n",row[0],row[1],row[2]);
	}
	if(!mysql_eof(result)){
		fprintf(stderr,"error:%s\n",mysql_error(mysql));
	}
	mysql_free_result(result);
	mysql_close(mysql);
	//mysql_library_end();
	return 0;
}
